<template>
  <div class="xinxi">
    <div class="w100" @click.stop="joinGame" v-if="!showLoading && (!userCode || !gameId)"></div>
    <x-loading v-if="showLoading"></x-loading>
    <ul>
      <li>
        <div class="icon" :class="teamImg"></div>
        <div class="text">{{teamName}}</div>
      </li>
      <li>
        <div class="icon paiming"></div>
        <div class="text">第{{ranknum}}名</div>
      </li>
      <li>
        <div class="icon" :class="levelImg"></div>
        <div class="text">{{level}}</div>
      </li>
      <li>
        <div class="icon jingyan"></div>
        <div class="text exp">{{exp}}分</div>
      </li>
      <li>
        <div class="icon shouyi"></div>
        <div class="text">{{total_profitrate}}%</div>
      </li>
      <li class="btns">
        <div class="duijiang" @click.stop="toDuijiang"><img src="~assets/images/duihuanBtn.png" alt=""></div>
        <div class="chakan flexCC">
          <a href="javascript:;" @click.stop.prevent="toDuijiangList"><img src="~assets/images/duihuanListBtn.png" alt=""></a>
          <a href="javascript:;" @click.stop.prevent="toJingyanzhi"><img src="~assets/images/jingyanMingxiBtn.png" alt=""></a>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
  import store from 'store/store'
  import sessionDAO from 'common/sessionDAO'
  import RemoteDAO from 'common/RemoteDAO'
  import Constant from 'common/constant'
  import XLoading from './XLoading'
  import {formatNowDate, toFixed, getUrlParam} from 'common/common.util'
  const TDAPP = window.TDAPP

  export default {
    name: 'xinxi',
    data () {
      return {
        teamName: '- -',
        ranknum: '- -',
        total_profitrate: '- -',
        level: '- -',
        exp: '- -',
        userCode: sessionDAO.getUserCode(),
        gameId: '', // 子比赛id
        showLoading: true, // 是否显示加载
        fetching: false // 是否已经开始获取接口
      }
    },
    components: {
      XLoading
    },
    computed: {
      loginStatus () {
        return store.state.loginStatus
      },
      teamImg () {
        let teamImg = 'huashan'
        switch (this.teamName) {
          case '华山派':
            teamImg = 'huashan'
            break
          case '武当派':
            teamImg = 'wudang'
            break
          case '少林派':
            teamImg = 'shaolin'
            break
          case '逍遥派':
            teamImg = 'xiaoyao'
            break
          default :
            break
        }
        return teamImg
      },
      levelImg () {
        let levelImg = 'level1'
        switch (this.level) {
          case '门童':
            levelImg = 'level1'
            break
          case '管事':
            levelImg = 'level2'
            break
          case '二代弟子':
            levelImg = 'level3'
            break
          case '一代弟子':
            levelImg = 'level4'
            break
          case '首席弟子':
            levelImg = 'level5'
            break
          case '掌门人':
            levelImg = 'level6'
            break
          default :
            break
        }
        return levelImg
      }
    },
    watch: {
      loginStatus () {
        this.userCode = sessionDAO.getUserCode()
        if (this.userCode && !this.fetching) {
          this.fetching = true
          this.getJinbi()
          this.getXinxi()
        } else {
          this.showLoading = false
        }
      }
    },
    mounted () {
      if (this.userCode && !this.fetching) {
        this.fetching = true
        this.getJinbi()
        this.getXinxi()
      } else if (!this.userCode) {
        this.showLoading = false
      }
//      this.test()
    },
    methods: {
      test () {
        this.userCode = '40668'
        this.getJinbi()
        this.getXinxi()
      },
      joinGame () {
        if (sessionDAO.getUserCode()) { // 未登录就登录
          window.location.href = 'gtjayyz://jumpfunc?funid=11007&pagename=team-game/game-detail&gameId=' + Constant.GLDH_ID + '&gameName=股林大会'
        } else {
          let tgCode = this.$route.query.tgCode || getUrlParam('tgCode')
          RemoteDAO.callNativeLogin(tgCode).then(res => {
            this.userCode = res.data.user_code
            sessionDAO.setUserCode(this.userCode)
            this.getJinbi()
            this.getXinxi(true)
          }, error => {
            this.$store.dispatch('setTip', {text: error.message})
          })
        }
        TDAPP.onEvent('23003', '', {userCode: this.userCode})
        TDAPP.send()
      },
      getJinbi () {
        // 查询今天是否已经获取积分
        let args = {
          userCode: this.userCode,
          getDate: formatNowDate()
        }
        RemoteDAO.callUC12000353(args).then(res => {
          if (res.data.body.length === 0) {
            this.$store.dispatch('setShowJinbi', true) // 显示金币弹窗
            // 发送获取积分的请求
            RemoteDAO.callUC12000109({userCode: this.userCode})
          }
        }, error => {
          this.$store.dispatch('setTip', {text: error.message})
        })
      },
      getXinxi (toJoin) {
        // toJoin: 是否是点击参加比赛后触发的
        // 用户信息查询 网易活动经验值与等级
        RemoteDAO.callUC12000114({userCode: this.userCode}).then(res => {
          this.level = res.data.body[0].USER_INFO
          this.exp = toFixed(res.data.body[1].USER_INFO, 0)
        }, error => {
          this.$store.dispatch('setTip', {text: error.message})
        })

        let params = {
          userCode: this.userCode,
          actionFlag: '3',
          pageSize: '1',
          pageNo: '1',
          totalFlag: '0',
          gameKind: '',
          gameID: Constant.GLDH_ID
        }
        console.log('callUC12008303-param:', params)
        // 我的比赛接口-获取门派
        RemoteDAO.callUC12008303(params).then(res => {
          this.showLoading = false
          if (res.data.body.length === 0) {
            // 未参加团队比赛 且要去参加 跳转
            if (toJoin) {
              window.location.href = 'gtjayyz://jumpfunc?funid=11007&pagename=team-game/game-detail&gameId=' + Constant.GLDH_ID + '&gameName=股林大会'
            }
          } else {
            this.gameId = res.data.body[0].GAME_ID
            this.teamName = res.data.body[0].GAME_NAME
            this.$store.dispatch('setGameId', this.gameId)
            this.$store.dispatch('setTeamName', this.teamName)
            // 获取收益率和排名
            RemoteDAO.callJCL11143({userCode: this.userCode, gameId: this.gameId}).then(res => {
              let mygame = res.data.body.record[0]
              this.total_profitrate = toFixed(mygame.total_profitrate, 2)
              this.ranknum = mygame.ranknum <= 0 ? '- -' : mygame.ranknum
            }, error => {
              this.$store.dispatch('setTip', {text: error.message})
            })
          }
        }, error => {
          this.showLoading = false
          this.$store.dispatch('setTip', {text: error.message})
        })
      },
      toDuijiang () {
        this.toWyPage('http://go.163.com/2017/0901/guotaijunan/shop.html?userCode=')
        TDAPP.onEvent('23004', '', {userCode: this.userCode})
        TDAPP.send()
      },
      toDuijiangList () {
        // this.toWyPage('http://test.go.163.com/go/2017/0901/guotaijunan/duihuan.html?userCode=')
        this.toWyPage('http://go.163.com/2017/0901/guotaijunan/duihuan.html?userCode=')
        TDAPP.onEvent('23005', '', {userCode: this.userCode})
        TDAPP.send()
      },
      toJingyanzhi () {
        this.$router.push('/jingyanzhi')
        TDAPP.onEvent('23006', '', {userCode: this.userCode})
        TDAPP.send()
      },
      toWyPage (url) {
        if (this.userCode) {
          RemoteDAO.decodeUsercode().then(res => {
            window.location.href = url + res
          }, error => {
            this.$store.dispatch('setTip', {text: error.messsge})
          })
        } else {
          let tgCode = this.$route.query.tgCode || getUrlParam('tgCode')
          RemoteDAO.callNativeLogin(tgCode).then(res => {
            this.userCode = res.data.user_code
            sessionDAO.setUserCode(this.userCode)
            RemoteDAO.decodeUsercode().then(res => {
              window.location.href = url + res
            }, error => {
              this.$store.dispatch('setTip', {text: error.messsge})
            })
          }, error => {
            this.$store.dispatch('setTip', {text: error.message})
          })
        }
      }
    }
  }
</script>
